package com.klamerek.rainbow.client.charts.geom;

public class MySegment {

	private MyPoint aPoint;
	private MyPoint bPoint;

	/**
	 * Constructor
	 * @param aPoint
	 * @param bPoint
	 */
	public MySegment(MyPoint aPoint, MyPoint bPoint){
		this.aPoint = aPoint;
		this.bPoint = bPoint;
	}

	public MyPoint getAPoint() {
		return aPoint;
	}

	public void setAPoint(MyPoint point) {
		aPoint = point;
	}

	public MyPoint getBPoint() {
		return bPoint;
	}

	public void setBPoint(MyPoint point) {
		bPoint = point;
	}

	/**
	 * Get the length.
	 * @return
	 */
	public double getLength(){
		double u = this.bPoint.getX() - this.aPoint.getX() ; 
		double v = this.bPoint.getY() - this.aPoint.getY() ;
		return Math.sqrt (u * u + v * v) ;
	}
	
	/**
	 * Return true if the line contains the point.
	 * @param point
	 * @param delta
	 * @return
	 */
	public boolean contains(MyPoint point, double delta){
		boolean result = false;
		MyLine line = new MyLine(this.aPoint,this.bPoint);
		if (line.contains(point, delta)){
			MyInterval interval1 = new MyInterval(this.aPoint.getX(),this.bPoint.getX());
			MyInterval interval2 = new MyInterval(this.aPoint.getY(),this.bPoint.getY());
			interval1.addDelta(delta);
			interval2.addDelta(delta);
			if (interval1.contains(point.getX()) && interval2.contains(point.getY())){
				result = true;
			}
		}
		return result;
	}

	/**
	 * Return true if the line contains the point.
	 * @param point
	 * @return
	 */
	public boolean contains(MyPoint point){
		boolean result = this.contains(point, 0.001);
		return result;
	}
	
	/**
	 * Return the middle of the segment.
	 * @return
	 */
	public MyPoint getMiddlePoint (){ 
		double x = (this.aPoint.getX() + this.bPoint.getX()) / 2.0 ; 
		double y = (this.aPoint.getY() + this.bPoint.getY()) / 2.0 ;
	    return new MyPoint(x,y);
	}

}
